package br.com.sisat.service;

import java.util.List;
import br.com.sisat.exception.LogicException;
import br.com.sisat.exception.ServiceException;
import br.com.sisat.modelo.PacienteDTO;
import br.com.sisat.persistencia.DAO.PacienteDAO;
import br.com.sisat.persistencia.conexao.ConnectionProvider;
import br.com.sisat.util.ObjectUtil;

public class PacienteService  {

	private static PacienteService service;
		
	public static PacienteService getInstance(){
		if(ObjectUtil.isEmpty(service)){
			service = new PacienteService();
		}
		return service;
	}

	public void incluir(PacienteDTO paciente) throws LogicException {
		java.sql.Connection conn = null;
		try{
			conn = ConnectionProvider.getDefaultConnection();
			PacienteDAO.getInstance().incluir(paciente, conn);
		}catch(Exception e){
			throw new RuntimeException(e);
		}finally{
			try {
				ConnectionProvider.fechaConexao(conn);
			} catch (ServiceException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
	
	public void alterar(PacienteDTO paciente) throws LogicException {
		java.sql.Connection conn = null;
		try{
			conn = ConnectionProvider.getDefaultConnection();
			PacienteDAO.getInstance().alterar(paciente, conn);
		}catch(Exception e){
			throw new RuntimeException(e);
		}finally{
			try {
				ConnectionProvider.fechaConexao(conn);
			} catch (ServiceException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
		
	public void excluir(PacienteDTO paciente) throws LogicException {
		java.sql.Connection conn = null;
		try{
			conn = ConnectionProvider.getDefaultConnection();
			PacienteDAO.getInstance().excluir(paciente);
		}catch(Exception e){
			throw new RuntimeException(e);
		}finally{
			try {
				ConnectionProvider.fechaConexao(conn);
			} catch (ServiceException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
	
	public List<PacienteDTO> consultar(PacienteDTO paciente) throws LogicException {
		try {
			return PacienteDAO.getInstance().consultar(paciente);
		} catch (Exception e) {
			e.printStackTrace();
		}		
		return null;
	}

	public boolean verificaExistenciaCPF(String deCPF) {
		try{
			int total = PacienteDAO.getInstance().verificaExistenciaCPF(deCPF);
			if (total >0)
				return true;
		}catch(Exception e){
			e.printStackTrace();
		}
		return false;
	}
			
}
